package com.i18n.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

@Controller
public class DemoController {
    
    private static Logger logger = LoggerFactory.getLogger(DemoController.class);
    
    @RequestMapping("/hello")
    public String hello(){
           return "/index";
    }
    
    /**
     * 中、英区域转换控制器
     * @param request
     * @param response
     * @param language
     * @return
     */
    @GetMapping("/checklanguage/{language}")
    public String  language(HttpServletRequest request, HttpServletResponse response,@PathVariable String language){
        //打印日志
        Locale locale= request.getLocale();
        logger.error(locale.toString());
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        //language=language.toLowerCase();
        logger.info("language:"+language);
        if(language==null||language.equals("")){
            return "index";
        }else{
            if(language.equals("CN")){
                localeResolver.setLocale(request, response, Locale.CHINA);//会话区域解析器
            }else if(language.equals("US")){
                localeResolver.setLocale(request, response, Locale.US);
            }else{
                localeResolver.setLocale(request, response, Locale.CHINA );
            }
        }
        return "index";
    }
}